package com.example.missingpersons.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.missingpersons.controller.DTO.LoginDTO;
import com.example.missingpersons.controller.DTO.RePwdDTO;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.missingpersons.utils.Result;
import com.example.missingpersons.service.IUserService;
import com.example.missingpersons.entity.User;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serial;

import org.springframework.web.bind.annotation.RestController;

/**
 * UserController 前端控制器
 * @author 张芸博
 * @since 2025-03-12
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @Operation(description = "新增或更新")
    public Result save(@RequestBody User user) {
        return Result.success(userService.saveOrUpdate(user));
    }

    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @Operation(description = "通过id删除")
    public Result delete(@PathVariable Integer id) {
        User user =userService.getById(id);
        return Result.success(userService.saveOrUpdate(user));
    }

    /**
    *  查询所有
    */
    @GetMapping("/all")
    @Operation(description = "查询所有")
    public Result findAll() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<User> list=  userService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    @Operation(description = "根据id查询")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    @Operation(description = "分页查询")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(userService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    @Operation(description = "用户登录")
    @PostMapping("/login")
    public Result save(@RequestBody LoginDTO loginDTO) {

        if (userService.Login(loginDTO) != null) {
            return Result.success(userService.Login(loginDTO));
        } else {
            return Result.error("账号不存在");
        }
    }

    @Operation(description = "登录获取验证码")
    @GetMapping("/verificationCode")
    public Result getCode() {
        return Result.success(userService.getCode());
    }

    @Operation(description = "重置密码验证码")
    @PostMapping("/mailCode")
    public Result mailRepwd(@RequestBody RePwdDTO rePwdDTO) {
        if (userService.mailCode(rePwdDTO)) {
            return Result.success(true);
        } else {
            return Result.error(false);
        }
    }

    @Operation(description = "重置密码")
    @PostMapping("/rePwd")
    public Result RePwd(@RequestBody RePwdDTO rePwdDTO) {
        if (userService.rePwd(rePwdDTO)) {
            return Result.success(userService.rePwd(rePwdDTO));
        }
        return Result.error("重置密码失败");
    }

    @Operation(description = "校验")
    @PostMapping("/verifyCode")
    public Result verifyCode(@RequestBody RePwdDTO rePwdDTO) {
        if (userService.verifyCode(rePwdDTO)) {
            return Result.success(true);
        } else {
            return Result.error(false);
        }
    }

    @Operation(description = "注册")
    @PostMapping("/signup")
    public Result signup(@RequestBody RePwdDTO rePwdDTO) {
        if (userService.signup(rePwdDTO)) {
            return Result.success(true);
        } else {
            return Result.error(false);
        }
    }
}

